package com.frank.oj.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 用于判题的异步线程池
 *
 * @author frank
 * @Date 2024/4/26
 */
@Configuration
@EnableAsync
public class JudgeAsyncTaskConfig {

    @Bean
    public Executor judgeTaskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2); //核心线程数
        executor.setMaxPoolSize(10); //最大线程数
        executor.setQueueCapacity(500); //队列容量
        executor.setKeepAliveSeconds(3); //线程活跃时间（S）
        executor.setThreadNamePrefix("JudgeExecutor-");//线程前缀名

        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //拒绝策略：运行任务，线程池满了，丢弃旧的任务，执行新的任务
        executor.initialize();

        return executor;
    }
}
